/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controler;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Set;
import lexicalanalyzer.AnalisadorLexico;
import lexicalanalyzer.Symbol;
import lexicalanalyzer.Token;
import syntacticanalyzer.AnalisadorSintatico;

/**
 *
 * @author Thiago
 */
public class Controler {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {

        String code = "codigo_de_exemplo_NELSON.txt";
        String automata = "automato_NELSON.txt";
        String reserved = "palavras_reservadas_NELSON.txt";

        System.out.println("========== Lexical Analysis ==========");
        //Conduct lexical analysis on code file and store the time taken.
        long time = System.currentTimeMillis();
        AnalisadorLexico al = new AnalisadorLexico(code, automata, reserved);
        time = System.currentTimeMillis() - time;

        //Print all errors found.
        ArrayList<String> erros = al.getErrors();
        if (erros.size() > 0) {
            System.out.println("Errors found:");
            System.out.println("");
            for (String erro : erros) {
                System.out.println(erro);
            }
            System.out.println("");
        }

        //Print the tokenized code and the time taken to produce it.
        System.out.println("Tokenized code after " + time + " miliseconds of total lexical analysis:");
        int lastLine = -1;
        for (Token item : al.getTokenList()) {
            if (lastLine != item.getLine()) {
                System.out.println("");
                System.out.print("Line " + item.getLine() + ": ");
            }
            System.out.print(item.getToken() + " ");
            lastLine = item.getLine();
        }
        System.out.println("");
        System.out.println("");

        HashMap<String, Symbol> symbolTable = al.getSymbolTable();
        Set<String> keys = symbolTable.keySet();
        System.out.println("Resulting symbol table:");
        System.out.println("");
        for (String key : keys) {
            Symbol s = symbolTable.get(key);
            System.out.println("ID: " + s.getId());
        }
        
        //
        System.out.println("");
        System.out.println("========== Syntactical Analysis ==========");
        String pathTable = "tabela_sintatica_NELSON.txt";
        String pathGrammar = "NELSON_BNF_CLEAN.txt";
        time = System.currentTimeMillis();
        AnalisadorSintatico as = new AnalisadorSintatico(al.getTokenList(), al.getSymbolTable(), pathTable, pathGrammar);
        time = System.currentTimeMillis() - time;
        System.out.println(time+" miliseconds");
    }
}
